
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

import { ListNode } from "./util";



export function isPalindrome(head: ListNode | null): boolean {
    function transToArry(headNode: ListNode | null) {
        const valueArray = [];
        while(headNode && (typeof headNode.val === 'number')) {
            valueArray.push(headNode.val);
            headNode = headNode.next;
        }
        return valueArray;
    }
    const valueArray = transToArry(head);
    const len = valueArray.length;
    const halfLen = len / 2;
    if (len === 1) {
        return true;
    }
    if (len % 2 === 1) {
        return false;
    }
    const leftArray = valueArray.slice(0, halfLen);
    const rightArray = valueArray.slice(halfLen);
    for (let i = 0; i < halfLen; i++) {
        if (leftArray[i] !== rightArray[halfLen - 1 - i]) {
            return false;
        }
    }
    return true;
};